﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Notepad_Clone
{
    class WordWrapMenuItem:MenuItem
    {


        public TextWrapping WordWrap
        {
            get { return (TextWrapping)GetValue(WordWrapProperty); }
            set { SetValue(WordWrapProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WordWrap.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WordWrapProperty =
            DependencyProperty.Register("WordWrap", typeof(TextWrapping), typeof(WordWrapMenuItem), new PropertyMetadata(TextWrapping.NoWrap));

        public WordWrapMenuItem()
        {
            this.Header = "_Word Wrap";

            MenuItem item = new MenuItem();
            item.Header = "_No wrap";
            item.Tag = TextWrapping.NoWrap;
            item.Click += this.MenuItemOnClick;
            this.Items.Add(item);

            item = new MenuItem();
            item.Header = "_Wrap";
            item.Tag = TextWrapping.Wrap;
            item.Click += this.MenuItemOnClick;
            this.Items.Add(item);

            item = new MenuItem();
            item.Header = "Wrap with _Overflow";
            item.Tag = TextWrapping.WrapWithOverflow;
            item.Click += this.MenuItemOnClick;
            this.Items.Add(item);
        }

        private void MenuItemOnClick(object sender, RoutedEventArgs e)
        {
            this.WordWrap = (TextWrapping)(e.Source as MenuItem).Tag;
        }

        protected override void OnSubmenuOpened(RoutedEventArgs e)
        {
            base.OnSubmenuOpened(e);
            foreach (MenuItem item in this.Items)
            {
                item.IsChecked = ((TextWrapping)item.Tag == this.WordWrap);
            }
        }
    }
}
